package com.jn.okhttp.views;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.jn.okhttp.R;
import com.jn.okhttp.contract.ITranslateContact;
import com.jn.okhttp.entity.Translate;
import com.jn.okhttp.presenter.TranslatePresenter;
import com.jn.okhttp.utils.LanguageUtils;


public class TranslateFragment extends Fragment implements View.OnClickListener , ITranslateContact.ITranslateView{
    private ITranslateContact.ITranslatePresenter tPresenter ;
    private Translate.DataTranslate tData ;

    private View view ;
    private static boolean INPUT = true ;
    private TextView tEng , tChi , tGet , tClear;
    private ImageButton tRep ;
    private EditText tInput ;
    private CardView tTra ;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null){
            view = inflater.inflate(R.layout.translate , container , false);
            init();
        }
        return view ;
    }

    private void init(){
        tEng = view.findViewById(R.id.tEng);
        tRep = view.findViewById(R.id.tRep);
        tChi = view.findViewById(R.id.tChi);
        tInput = view.findViewById(R.id.tInput);
        tTra = view.findViewById(R.id.toTranslate);
        tGet = view.findViewById(R.id.tGet);
        tClear = view.findViewById(R.id.tClear);
        tRep.setOnClickListener(this);
        tTra.setOnClickListener(this);
        tClear.setOnClickListener(this);
        tPresenter = new TranslatePresenter(this , getContext());
    }
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tRep){
            if (INPUT){
                INPUT = false ;
                startAnimation1();
                startAnimation2();
            }else {
                INPUT = true ;
                startAnimation3();
                startAnimation4();
            }
        } else if (id == R.id.toTranslate) {
            String input = tInput.getText().toString().trim();

            if (!input.isEmpty()) {
                boolean isEnglish = LanguageUtils.isPureEnglish(input);

                if ((INPUT && isEnglish) || ((!INPUT) && (!isEnglish))){
                    //tPresenter.getData(input);
                    tPresenter.postData(input);
                }else {
                    Toast.makeText(getActivity(), "格式错误", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(getActivity(), "请输入内容", Toast.LENGTH_SHORT).show();
            }
        }else if (id == R.id.tClear){
            tInput.setText("");
            tGet.setText("");
            tInput.setHint("原文");
            tGet.setHint("翻译");
            tInput.clearFocus();
        }
    }

    private void startAnimation1() {
        //左右移动
        ObjectAnimator move = ObjectAnimator.ofFloat(tEng, "translationX", 0f, 400f);
        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(move);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }
    private void startAnimation2() {
        //左右移动
        ObjectAnimator move = ObjectAnimator.ofFloat(tChi, "translationX", 0f, -400f);
        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(move);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }
    private void startAnimation3() {
        //左右移动
        ObjectAnimator move = ObjectAnimator.ofFloat(tEng, "translationX", 400f, 0f);
        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(move);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }
    private void startAnimation4() {
        //左右移动
        ObjectAnimator move = ObjectAnimator.ofFloat(tChi, "translationX", -400f, 0f);
        // 使用AnimatorSet来组合这些动画
        AnimatorSet set = new AnimatorSet();
        set.playTogether(move);
        set.setDuration(300); // 设置动画持续时间为1秒
        set.start(); // 启动动画
    }

    @Override
    public void onHttpLoading() {

    }

    @Override
    public void onHttpFail(String... msg) {

    }

    @Override
    public void onHttpTimeout() {

    }

    @Override
    public void onHttpTokenExpiration() {

    }

    @Override
    public void showData(Translate data) {
        tData = data.getResult();
        Log.d("1234567890", "1234567890987654321");
        String target = tData.getTargetText();
        Log.d("获取到的翻译", target);
        tGet.setText(target);
    }

}
